#import "ALEntryTemplate.h"
#import "ALEntry.h"
#import "ALElement.h"
#import "ALElementTemplate.h"


static NSArray* ALEntryTemplateElementSortDescriptors;
static NSDictionary* ALEntryTemplateDescriptionAttributes;
static NSDictionary* ALEntryTemplateLabelAttributes;


@implementation ALEntryTemplate

+(void)initialize {
	ALEntryTemplateElementSortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:ALLineNumberKey ascending:YES]];
	ALEntryTemplateLabelAttributes = [NSDictionary dictionaryWithObject:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]] forKey:NSFontAttributeName];
	ALEntryTemplateDescriptionAttributes = [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]] forKey:NSFontAttributeName];
}

-(void)setPrimaryElementTemplate:(ALElementTemplate *)aTemplate {
    [self willChangeValueForKey:@"primaryElementTemplate"];
    [self setPrimitivePrimaryElementTemplate:aTemplate];
    [self didChangeValueForKey:@"primaryElementTemplate"];
    
    if ([aTemplate lineNumberValue] != 0)
        [self setPrimaryIsDefaultValue:NO];
}

+(NSSet *)keyPathsForValuesAffectingLabel {
	return [NSSet setWithObject:@"firstElement.label"];
}

-(NSString *)label {
	return [[self firstElement] label];
}

-(void)setLabel:(NSString *)label {
	NSString *labelCopy = [[label copy] autorelease];
	[[self firstElement] setLabel:labelCopy];
}

-(ALElementTemplate *)firstElement {
	if ([[self elementTemplates] count] == 0)
		return nil;
	NSArray *sortedElements = [[self elementTemplates] sortedArrayUsingDescriptors:ALEntryTemplateElementSortDescriptors];
	return [sortedElements objectAtIndex:0];
}

-(void)setFirstElement:(ALElementTemplate *)element {}

-(ALEntry *)stamp {
	ALEntry *stampedEntry = [ALEntry insertInManagedObjectContext:[self managedObjectContext]];
	[stampedEntry setTemplate:self];
	for (ALElementTemplate *elementTemplate in [self elementTemplates]) {
		ALElement *element = [elementTemplate stamp];
		[element setEntry:stampedEntry];
	}
    [stampedEntry setTags:[self tags]];
	return stampedEntry;
}

-(void)insertElementTemplate:(ALElementTemplate *)elementTemplate atLineNumber:(short)lineNumber {
	for (ALElementTemplate *currentElementTemplate in [self elementTemplates]) {
		short currentLineNumber = [[currentElementTemplate lineNumber] shortValue];
		if (currentLineNumber >= lineNumber)
			[currentElementTemplate setLineNumber:[NSNumber numberWithShort:currentLineNumber + 1]];
	}
	[elementTemplate setLineNumber:[NSNumber numberWithShort:lineNumber]];
	[self addElementTemplatesObject:elementTemplate];
	for (ALEntry *currentEntry in [self entries]) {
		[currentEntry addElementsObject:[elementTemplate stamp]];
	}
}

+(NSSet *)keyPathsForValuesAffectingAttributedLabelAndDescription {
	return [NSSet setWithObjects:@"label", @"entryDescription", nil];
}

-(NSAttributedString *)attributedLabelAndDescription {
	NSMutableAttributedString *mutableResultString = [[NSMutableAttributedString alloc] initWithString:[self label] attributes:ALEntryTemplateLabelAttributes];
	NSString *descriptionString = [NSString stringWithFormat:@" - %@", [self entryDescription]];
	[mutableResultString appendAttributedString:[[NSAttributedString alloc] initWithString:descriptionString attributes:ALEntryTemplateDescriptionAttributes]];
	return mutableResultString;
}

@end
